package com.kanshu.ksgb.fastread.doudou.module.book.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.util.JsonUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.BookService;
import com.kanshu.ksgb.fastread.doudou.module.book.retrofit.requestparams.ComentRequestParams;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Freedom on 17/12/29.
 */

public class CommentActivity extends BaseActivity {

    @BindView(R.id.edit_info)
    EditText editInfo;
    @BindView(R.id.submit)
    Button submit;
    String mBookId;
    AlertDialog mDialog;

    private void parseIntent() {
        mBookId = getIntent().getStringExtra("book_id");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_comment_layout);
        ButterKnife.bind(this);
        setTitle("评论");
        parseIntent();
    }

    @OnClick({R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submit:
                submit();
                break;
            default:
                break;
        }
    }

    private void submit() {
        String s = editInfo.getText().toString();
        if (TextUtils.isEmpty(s)) {
            showDlg("评论内容不能为空！", false);
            return;
        }
        if (!NetUtils.isNetworkAvailable(this)) {
            showDlg("网络错误/网络无法连接", false);
            return;
        }
        RetrofitHelper helper = RetrofitHelper.getInstance();
        BookService mBookService = helper.createService(BookService.class);
        ComentRequestParams params = new ComentRequestParams();
        params.book_id = mBookId;
        params.comment_content = s;
        Map<String, String> map = new HashMap<>();
        map.put("book_id", mBookId);
        map.put("comment_content", s);
        map.put("parent_id", "0");
        mBookService.submit(map).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                showDlg("评论成功！", true);
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                showDlg("评论失败！", false);
            }
        });
    }

    private void showDlg(String s, final boolean finishPage) {
        if (isFinishing()) {
            return;
        }
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage(s);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mDialog.dismiss();
                if (finishPage && !CommentActivity.this.isFinishing()) {
                    finish();
                }
            }
        });
        mDialog = builder.show();
    }
}


